package cs;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class ReservationSystem {
    private List<MeetingRoom> meetingRooms; // 会议室列表
    private List<Reservation> reservations; // 预订信息列表

    public ReservationSystem(List<MeetingRoom> meetingRooms) {
        this.meetingRooms = meetingRooms;
        this.reservations = new ArrayList<>();
    }

    /**
     * 查看所有会议室
     */
    public void showAllMeetingRooms() {
        System.out.println("---所有会议室列表---");
        System.out.println(String.format("%-10s %-10s %s", "名称", "容纳人数", "多媒体设备"));
        meetingRooms.forEach(System.out::println);
        System.out.println("-------------");
    }

    /**
     * 预订会议室
     */
    public void reserveMeetingRoom() {
        Scanner sc = new Scanner(System.in);

        // 展示会议室列表
        for (int i = 0; i < meetingRooms.size(); i++) {
            System.out.printf("%d. %s%n", i + 1, meetingRooms.get(i));
        }

        // 用户选择会议室
        System.out.print("请选择要预订的会议室编号：");
        int roomNo = sc.nextInt();
        if (roomNo < 1 || roomNo > meetingRooms.size()) {
            System.out.println("无效的会议室编号！");
            return;
        }
        MeetingRoom selectedRoom = meetingRooms.get(roomNo - 1);

        // 输入预订日期和时间段
        System.out.print("请输入预订日期(yyyy-MM-dd)：");
        LocalDate date = LocalDate.parse(sc.next());
        System.out.print("请输入预订时间段(例如：10:00-11:00)：");
        String[] time = sc.next().split("-");
        LocalTime startTime = LocalTime.parse(time[0]);
        LocalTime endTime = LocalTime.parse(time[1]);

        // 检查是否冲突
        if (isAvailable(selectedRoom, date, startTime, endTime)) {
            reservations.add(new Reservation(selectedRoom, date, startTime, endTime));
            System.out.println("预订成功！");
        } else {
            System.out.println("该时间段已被预订，请选择其他时间段或会议室！");
        }
    }

    /**
     * 判断会议室是否可用
     */
    private boolean isAvailable(MeetingRoom room, LocalDate date, LocalTime startTime, LocalTime endTime) {
        return reservations.stream()
                .filter(r -> r.getMeetingRoom().equals(room))
                .filter(r -> r.getDate().equals(date))
                .noneMatch(r -> !(r.getEndTime().isBefore(startTime) || r.getStartTime().isAfter(endTime)));
    }

    /**
     * 查看预订信息
     */
    public void showResInfo() {
        System.out.println("---所有预订信息---");
        meetingRooms.forEach(room -> {
            List<Reservation> roomReservations = reservations.stream()
                    .filter(r -> r.getMeetingRoom().equals(room))
                    .collect(Collectors.toList());
            if (!roomReservations.isEmpty()) {
                System.out.println(room.getName());
                roomReservations.forEach(System.out::println);
            } else {
                System.out.println(room.getName() + "：无预订信息");
            }
        });
        System.out.println("------------");
    }

    /**
     * 查看指定时间段内的可用会议室
     */
    public void selectRoomByTime() {
        Scanner sc = new Scanner(System.in);

        // 输入日期和时间段
        System.out.print("请输入日期(yyyy-MM-dd)：");
        LocalDate date = LocalDate.parse(sc.next());
        System.out.print("请输入时间段(例如：10:00-11:00)：");
        String[] time = sc.next().split("-");
        LocalTime startTime = LocalTime.parse(time[0]);
        LocalTime endTime = LocalTime.parse(time[1]);

        // 查询可用会议室
        List<MeetingRoom> availableRooms = meetingRooms.stream()
                .filter(room -> isAvailable(room, date, startTime, endTime))
                .collect(Collectors.toList());

        // 输出结果
        System.out.println("---可用会议室列表---");
        if (availableRooms.isEmpty()) {
            System.out.println("当前时间段内无可用车间！");
        } else {
            availableRooms.forEach(System.out::println);
        }
    }
}

